home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / text / hyper / hsc_source.lha / hsc / source / ugly / utime.c < prev    next >
C/C++ Source or Header  |  1996-07-30  |  4KB  |  170 lines

  1. /*
  2.  * ugly/utime.c
  3.  *
  4.  * misc. time-handling functions
  5.  *
  6.  * Copyright (C) 1994,95,96  Thomas Aglassinger
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * updated: 30-Jul-1996
  23.  * created:  6-Jun-1996
  24.  *
  25.  *-------------------------------------------------------------------
  26.  *
  27.  */
  28.  
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <time.h>
  33.  
  34. #include "utypes.h"
  35. #include "umemory.h"
  36. #include "ustring.h"
  37. #include "expstr.h"
  38.  
  39. #define NOEXTERN_UGLY_UTIME_H
  40. #include "utime.h"
  41.  
  42. /* debugging defines */
  43. #ifdef D
  44. #undef D
  45. #endif
  46.  
  47. #if DEBUG_UGLY_TIME
  48. #define D(x) x
  49. #define DUT "*utime * "
  50. #else
  51. #define D(x)                    /* nufin */
  52. #endif
  53.  
  54. BOOL time2estr(EXPSTR * dest, struct tm *time)
  55. {
  56. #define TIMEBUF_INC_STR "12345678901234567890"
  57.  
  58.     /*localtime(&(hp->start_time)) ); */
  59.     BOOL ok = TRUE;
  60.     size_t strftrc = 0;
  61.  
  62.     /* reset destination string */
  63.     set_estr(dest, TIMEBUF_INC_STR);
  64.  
  65.     /* write time to string */
  66.     while (ok && !strftrc)
  67.     {
  68.  
  69.         /* expand dest */
  70.         ok = app_estr(dest, TIMEBUF_INC_STR);
  71.  
  72.         D(fprintf(stderr, DUT "dest: inc+%ld\n",
  73.                   strlen(TIMEBUF_INC_STR)));
  74.  
  75.         /* write string */
  76.         strftrc = strftime(estr2str(dest), estrlen(dest),
  77.                            UGLY_TIME_TEMPLATE_STRFTIME, time);
  78.  
  79.     }
  80.  
  81.     D(if (!ok) fprintf(stderr, DUT "time2estr: FAILED\n"));
  82.  
  83.     return (ok);
  84. }
  85.  
  86. VOID clrtime(struct tm * time)
  87. {
  88.     time->tm_sec = 0;
  89.     time->tm_min = 0;
  90.     time->tm_hour = 0;
  91.     time->tm_mday = 0;
  92.     time->tm_mon = 0;
  93.     time->tm_year = 0;
  94.     time->tm_wday = 0;
  95.     time->tm_yday = 0;
  96.     time->tm_isdst = 0;
  97. }
  98.  
  99. BOOL str2time(struct tm *time, STRPTR timestr)
  100. {
  101.     BOOL ok = FALSE;
  102.     int scanrc = EOF;
  103.  
  104.     printf("testing ugly time funtions\n");
  105.  
  106.     clrtime(time);
  107.  
  108.     /* convert string to time */
  109.     scanrc = sscanf(timestr, UGLY_TIME_TEMPLATE_SSCANF,
  110.                     &time->tm_year, &time->tm_mon, &time->tm_mday,
  111.                     &time->tm_hour, &time->tm_min, &time->tm_sec);
  112.  
  113.     if (scanrc != EOF)
  114.     {
  115.         time_t ttime;
  116.  
  117.         /* adjust values */
  118.         time->tm_year -= 1990;
  119.         time->tm_mon--;
  120.         time->tm_mday--;
  121.  
  122.         /* fill rest of time-structure */
  123.         ttime = mktime(time);
  124.  
  125.         if (ttime != ((time_t) - 1))
  126.         {
  127.  
  128.             struct tm *ltime = localtime(&ttime);
  129.  
  130.             D(
  131.                  {
  132.  
  133.                  STRARR timebuf2[200];
  134.                  strftime(timebuf2, 200, UGLY_TIME_TEMPLATE_STRFTIME, ltime);
  135.                  fprintf(stderr, DUT "ltime = `%s'\n", timebuf2);
  136.  
  137.                  }
  138.             );
  139.  
  140.             if (ltime)
  141.             {
  142.                 memcpy(time, ltime, sizeof(struct tm));
  143.                 ok = TRUE;
  144.             }
  145.             else
  146.             {
  147.                 D(fprintf(stderr, DUT "str2time: localtime() FAILED\n",
  148.                           strlen(TIMEBUF_INC_STR)));
  149.             }
  150.  
  151.         }
  152.         else
  153.         {
  154.  
  155.             D(fprintf(stderr, DUT "str2time: mktime() FAILED\n",
  156.                       strlen(TIMEBUF_INC_STR)));
  157.  
  158.         }
  159.     }
  160.     else
  161.     {
  162.  
  163.         D(fprintf(stderr, DUT "str2time: sscanf() FAILED\n",
  164.                   strlen(TIMEBUF_INC_STR)));
  165.     }
  166.  
  167.     return (ok);
  168. }
  169.  
  170.